<?php
require_once "db.class.php";
require_once "config.inc.php";

$DB = new db();
if ($DB)
    $DB->connect(TT_DBHOST, TT_DBUSER, TT_DBPW, TT_DBNAME, TT_DBCHARSET);
else
    exit("cann't init database");

function fs_location($pid, $ext){
    $dir = substr($pid, 0, 2);
    return "/upload/$dir/$pid.$ext";
}

function pic_insert($pid, $ext){
    global $DB;
    $url = fs_location($pid, $ext);
    $query = "INSERT INTO ".TT_DBTABLEPRE. "pic(`id`, `url`) VALUES ('$pid', '$url');";
    $DB->query($query);
    return $pid;
}
function pic_info($pid){
    global $DB;
    $query = sprintf("SELECT * FROM ".TT_DBTABLEPRE. "pic WHERE id = '$pid';");
    $rst = $DB -> fetch_first($query);
    $marks = json_decode (base64_decode($rst['marks']));
    $o = array(
        'id' => $rst['id'],
        'url' => $rst['url'],
        'marks' => $marks,
        );
    return json_encode($o);
}

?> 

